/* 
 * Copyright (C) 2012 UTSChedule
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package utsc.apps.utschedule.ui.activities;

import com.actionbarsherlock.app.SherlockActivity;

import utsc.apps.utschedule.R;
import utsc.apps.utschedule.UTSCheduleApp;
import utsc.apps.utschedule.UTSCheduleHomeActivity;
import utsc.apps.utschedule.classes.Timetable;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

/**
 * This is a splash screen when first time open the app. It also initializes some basic resources.
 *
 */
public class SplashScreenActivity extends SherlockActivity{


	GetTimetableTask _task;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splashscreen);
		_task=new GetTimetableTask(this);
		_task.execute();
	}
	
	public void startup() {
		Animation anim = AnimationUtils.loadAnimation(this, R.anim.alpha);
        anim.reset();
        LinearLayout l=(LinearLayout) findViewById(R.id.splash_layout);
        l.clearAnimation();
        l.startAnimation(anim);
	}
	
	public void onAttachedToWindow() {
        super.onAttachedToWindow();
        Window window = getWindow();
        window.setFormat(PixelFormat.RGBA_8888);
    }
	
	
	 private static class GetTimetableTask extends AsyncTask<Void, Void, Void>{
			protected static final int LOADING_TIME=2000;
			protected int _loadingCounter=0;
			protected static final int LOADING_INTERVAL=100;
			protected boolean _active=true;
			
			private long _startDataLoad, _endDataLoad;
	    	Context _context;
	    	
	    	public GetTimetableTask(Activity context){
	    		_context=context;
	    	}
	    	@Override
	    	protected void onPreExecute() {
	    		_startDataLoad=System.currentTimeMillis();
	    	}
	    	
			@Override
			protected Void doInBackground(Void... params) {
				UTSCheduleApp app=(UTSCheduleApp) ((Activity)_context).getApplication();
				if (app!=null) {
					Timetable timetable=app.get_currentActiveTimetable();
					app.refreshCurrentTimetable();
					if (timetable!=null) {
						timetable.refreshCourseList();
					}
				}
				
				return null;
			}
	    	
			@Override
			protected void onPostExecute(Void result) {
				_endDataLoad=System.currentTimeMillis();
				_loadingCounter=(int) (_endDataLoad-_startDataLoad);
				new Thread(){
					public void run() {
						try {
							while (_active && (_loadingCounter < LOADING_TIME)) {
								sleep(LOADING_INTERVAL);
								if (_active) {
									_loadingCounter+=LOADING_INTERVAL;
								}
							}
						} catch (InterruptedException e) {
						}finally{
							if (!_active) {
								
								Activity activity=(Activity)_context;
								activity.finish();
							}
							else {
								Intent startupIntent=new Intent(_context, UTSCheduleHomeActivity.class);
								Activity activity=(Activity)_context;
								activity.startActivity(startupIntent);
								activity.finish();
							}
							cancel(true);
						}
					}
				}.start();
				
			}
	    }
	 
	 @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		 if (keyCode==KeyEvent.KEYCODE_BACK) {
			 _task._active=false;
			 return true;
		}
		 return super.onKeyDown(keyCode, event);
	}
}
